package sp.service;

import sp.dao.BaseDao;
import sp.dao.Page;
import sp.dao.UserDao;
import sp.dao.UserDaoImpl;
import sp.entity.User;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService{
    UserDao userDao = new UserDaoImpl();
    @Override
    public List<User> find(User user) {
        return  userDao.find(user);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }


    @Override
    public Page<User> findByPage(String userId, String userName, int page, int rows) {
        Map<String,String> params = new HashMap<>();
        if(userId !=null) if(userId.trim().length()>0) params.put("userId",userId);
        if(userName !=null) if(userName.trim().length()>0) params.put("userName",userName);
        return userDao.findByPage(params,page,rows);
    }

    @Override
    public void delete(String id) {
        boolean autocommit = false;
        try {
            autocommit = BaseDao.begin();
            userDao.delete(id);
            BaseDao.commit(autocommit);
        } catch (SQLException e) {
            try {
                BaseDao.rollback(autocommit);
            } catch (SQLException ex) {
                ex.printStackTrace();
                throw  new RuntimeException(ex);
            }
        }
    }
}
